package com.dao.impl;

import com.bean.BhUS;
import com.bean.BhUser;
import com.bean.BhWorkData;
import com.bean.Studentwork;
import com.dao.Studentworkdao;
import com.util.DB;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Studentworkdapimpl implements Studentworkdao {
    QueryRunner runner = new QueryRunner();
    Connection conn= DB.getConn();

    @Override
    public List<BhWorkData> sely(int tid) throws SQLException {
        String sql="select MONTH(date) as 'month' from studentwork where tid=? GROUP BY MONTH(date)";
        List<BhWorkData> list = runner.query(conn, sql,new BeanListHandler<>(BhWorkData.class),tid);
        return list;
    }
    @Override
    public List<BhWorkData> selt(int tid,int t) throws SQLException {
        String sql="select Day(date) as 'day' from studentwork where tid=?  GROUP BY date HAVING MONTH(date)=?";
        List<BhWorkData> list = runner.query(conn, sql,new BeanListHandler<>(BhWorkData.class),tid,t);
        System.out.println(list);
        return list;
    }

    @Override
    public List<BhUS> selxx(int tid, String uclass, int y, int t) throws SQLException {
        String sql="select * from studentwork sw INNER JOIN bhuser b ON sw.uclass = b.uClass AND sw.uid=b.uid where sw.tid=? and sw.uclass=? HAVING MONTH(sw.date)=? AND DAY(sw.date)=?";
        List<BhUS> list = runner.query(conn, sql,new BeanListHandler<>(BhUS.class),tid,uclass,y,t);
        System.out.println(list);
        return list;
    }

    @Override
    public List<BhUser> selname(List<Studentwork> list) throws SQLException {
        List<BhUser> list1 = new ArrayList<>();
        String sql="select uName from BhUser where uid=? ";
        BhUser bu = null;
        for (int i=0; i < list.size() ; i++){
            bu = runner.query(conn, sql,new BeanHandler<>(BhUser.class),list.get(i).getUid());
            list1.add(bu);
        }
        System.out.println(list1);
        return list1;
    }
}
